#!/usr/bin/python3
#-*- coding:UTF-8 -*-
"""
@file            dump-maker-file.py
@author          mening
@date            2024-01-15 11:27:49
@copyright       Copyright (c) 2024 by 北京长扬科技有限公司, ALL Rights Reserved.
@details         从“dump-basic-file-continue.txt”中获取要生成dump的库，并生成dump.
"""
import os
import sys
import re

def get_sofile_name(sofilename):
    """
    get the .so file name
    ie: foo.so.0.0 -> foo.so
    """
    return  re.findall(r'.*.so', sofilename)[0]

def dumper_by_debuginfo(verdir):
    """
    generate .dump file by debuginfo .rpm file
    """
    cnt = 0
    cnt_basic = 0
    os.chdir(verdir)
    sofiles = []
    with open('dump-basic-file-continue.txt') as file:
        lines = file.readlines()
        for line in lines:
            sofiles.append(line.strip())

    if len(sofiles) > 0:
        for sofile in sofiles:
            (sofile_path, sofile_name) = os.path.split(sofile)
            sofile_name = get_sofile_name(sofile_name)
            os.system('abi-dumper ' + sofile + ' --search-debuginfo=' + verdir + '/usr/lib/debug/' + ' -o ' + 'ABI-' + sofile_name + '.dump')
            cnt_basic += 1
        print("Found .so file in basic-continue path:%d" %cnt_basic)

if __name__ == '__main__':
    """
    main
    1. create a directory named sys.argv[1] (libfoo) in sys.argv[2] (/root/checkdir/);
    2. put the rpm files and debuginfo files of 2 versions in libfoo;
    3. run 'python abichecker.py 'libfoo' '/root/checkdir/'
    4. the results are saved in /root/checkdir/libfoo/compat_reports
    """
    dumper_by_debuginfo(os.getcwd())